﻿
-- =============================================
-- Author:		Shane
-- Create date: 2014-12-22 16:55:28
-- Description:	
-- =============================================
CREATE PROCEDURE [dbo].[proc_CallCenter_GetClientCompanyID] 
	-- Add the parameters for the stored procedure here
	@IP_Address VARCHAR(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	--DECLARE @IP_Address varchar(255);
	DECLARE @CompanyID int;
  
    --SELECT @IP_Address = client_net_address
    --FROM sys.dm_exec_connections
    --WHERE Session_id = @@SPID;
	
	--SELECT @IP_Address AS IP;
	
	SELECT TOP 1 @CompanyID=CompanyID FROM CallCenter_Clients WHERE IP=@IP_Address; --根据IP查询CompanyID
	
	IF @CompanyID IS NOT NULL 
		UPDATE CallCenter_Clients SET LastConnectTime=GETDATE() WHERE CompanyID=@CompanyID AND IP=@IP_Address;
	--ELSE (SELECT 0)
	
	SELECT ISNULL(@CompanyID,-1) AS CompanyID;
END

--SELECT TOP 1 CompanyID FROM CallCenter_Clients WHERE IP='222.171.225.189'